package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExCategory;
import com.example.ymm.modules1.entity.hl.ex.ExQuestion;
import com.example.ymm.modules1.entity.hl.ex.ExTeam;
import com.example.ymm.modules1.mapper.ex.ExCategoryMapper;
import com.example.ymm.modules1.query.hl.ex.ExCategoryQueryCriteria;
import com.example.ymm.modules1.query.hl.ex.ExQuestionQueryCriteria;
import com.example.ymm.modules1.service.hl.ex.ExCategoryService;
import com.example.ymm.modules1.service.hl.ex.ExQuestionService;
import com.example.ymm.modules1.service.hl.ex.ExTeamService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class ExCategoryServiceImpl extends BaseServiceImpl<ExCategoryMapper, ExCategory> implements ExCategoryService {

    @Autowired
    ExQuestionService exQuestionService;

    @Autowired
    ExTeamService exTeamService;

    @Override
    public TableList<ExCategory> list(ExCategoryQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExCategory> page = new Page<ExCategory>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExCategory.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<ExCategory> tableList=new TableList(page);
        return tableList;
    }

    public List<ExCategory> queryAll(ExCategoryQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExCategory.class, criteria);
//        predicate.orderByDesc("create_time");
        List<ExCategory> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<ExCategory> getCategoryTreeList(ExCategoryQueryCriteria criteria) {

        List<ExCategory> arr = queryAll(criteria);
        List<ExCategory> menus=new ArrayList<>();
        arr.forEach(e->{
            if("0".equals(e.getPid())){
                forEachList(e,arr);
                menus.add(e);
            }
        });
        return menus;
    }
    public void forEachList(ExCategory parent, List<ExCategory> list){

        List<ExCategory> children=new ArrayList<>();
        list.forEach(e->{
            if(parent.getCategoryId().equals(e.getPid())){
                children.add(e);
            }
        });

        if(UtilValidate.isNotEmpty(children)){
            children.sort(new Comparator<ExCategory>() {
                @Override
                public int compare(ExCategory o1, ExCategory o2) {
                    return o1.getSort()-o2.getSort();
                }
            });
            parent.setChildren(children);

            children.forEach(r->{
                forEachList(r,list);
            });
        }

    }

    @Override
    public void categoryRemove(ExCategory category) {

        removeById(category);
        ExQuestionQueryCriteria criteria=new ExQuestionQueryCriteria();
        criteria.setCategoryId(category.getCategoryId());
        List<ExQuestion> questions = exQuestionService.queryAll(criteria);
        for(ExQuestion question:questions){
            question.setCategoryId(null);
            question.setCategoryName(null);
            exQuestionService.saveOrUpdate(question);
        }
    }

    @Override
    public void initCategory() {
        List<ExTeam> teams = exTeamService.queryAll(null);
        int sort=10;
        for (ExTeam team : teams) {
            sort++;
            ExCategoryQueryCriteria criteria=new ExCategoryQueryCriteria();
            criteria.setPid("0");
            criteria.setTeamId(team.getTeamId());
            sort++;
            List<ExCategory> exCategories = queryAll(criteria);
            if(UtilValidate.isEmpty(exCategories)){
                ExCategory p_category=new ExCategory();
                p_category.setPid("0");
                p_category.setCategoryName(team.getDeptName());
                p_category.setTeamId(team.getTeamId());
                p_category.setTeamName(team.getDeptName());
                p_category.setSort(sort);
                saveOrUpdate(p_category);

                ExCategory c_category=new ExCategory();
                c_category.setPid(p_category.getCategoryId());
                c_category.setCategoryName("2025年年中"+team.getDeptName()+"试题");
//                c_category.setTeamId(team.getTeamId());
//                c_category.setTeamName(team.getDeptName());
                c_category.setSort(1);
                c_category.setDifficultyLevel(1);
                saveOrUpdate(c_category);
            }
        }
    }

    @Override
    public ExCategory findTopParent(String categoryId) {
        ExCategory byId = getById(categoryId);
        if(byId.getPid().equals("0")){
            return byId;
        }
        ExCategory p = getById(byId.getPid());
        ExCategory result = null;
        if(UtilValidate.isNotEmpty(p)){
            if(p.getPid().equals("0")){
                result=p;
            }else{
               return findTopParent(p.getPid());
            }
        }
        return result;
    }
}
